package day_2022_9_to_12.Day;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * https://leetcode.cn/problems/lru-cache/solution/yuan-yu-linkedhashmapyuan-ma-by-jeromememory/
 * LinkedHashMap 源码分析
 * @author haomin
 * @date 2022/09/08 20:58
 **/
public class Day6_9_Num146_LRUCache {
    class LRUCache extends LinkedHashMap<Integer, Integer> {
        private int capacity;
        public LRUCache(int capacity) {
            super(capacity, 0.75f, true);
            this.capacity = capacity;
        }

        public int get(int key) {
            return super.getOrDefault(key, -1);
        }

        public void put(int key, int value) {
            super.put(key, value);
        }

        @Override
        protected boolean removeEldestEntry(Map.Entry<Integer, Integer> eldest) {
            return size() > capacity;
        }
    }
}